<!DOCTYPE html>
<html>
<head>
    <#import "../common/common.macro.ftl" as netCommon>
    <title>${title!"艺术上海"}</title>
    <meta charset="utf-8" />
    <meta name="keywords" content="${keywords?if_exists}" />
    <meta name="description" content="${description?if_exists}" />
    <meta name="renderer" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <link href="/static/css/bootstrap.mini.css" rel="stylesheet" />
    <link href="/static/css/style.css" rel="stylesheet" />
    <link href="/static/css/common/index.css" rel="stylesheet" />

    <link href="/static/css/footer/index.css" rel="stylesheet" />
    <link href="/static/css/font/font-awesome.min.css" rel="stylesheet" />
    <link href="/static/css/font/font-awesome.min.css" rel="stylesheet" />
    <link href="/static/css/font-awesome.min.css" rel="stylesheet" type="text/css">

    <style>
        .container-p{
            width: 1200px;
        }
        .padding-y{
            margin: 20px;
        }
        .form-group label{
            margin: 10px 0px;
        }

        .table > thead > tr > th, .table > tbody > tr > th, .table > tfoot > tr > th, .table > thead > tr > td, .table > tbody > tr > td, .table > tfoot > tr > td {
            padding: 0.75rem;
            vertical-align: middle;
            border-top: 1px solid #dee2e6;
        }

    </style>
</head>
<body>
<!-- header -->
<@netCommon.commonHeader "首页" menu i18n/>

<section class="section-pagetop bg" style="margin-top: 110px">
    <div class="container container-p">
        <h2 class="title-page">我的收货地址</h2>
    </div> <!-- container //  -->
</section>

<section class="section-content padding-y ">
    <div class="container container-p">

        <!-- =========================  COMPONENT MYORDER 1 ========================= -->
        <div class="row">
            <aside class="col-md-3">
                <!--   SIDEBAR   -->
                <ul class="list-group">
                    <a class="list-group-item " href="/user/order"> 我的订单 </a>
                    <a class="list-group-item" href="/user/setting"> 个人设置 </a>
                    <a class="list-group-item active" href="/user/address"> 我的收货地址 </a>
                </ul>


            </aside>
            <main class="col-md-9">

                <article class="card">

                    <div class="card-body">
                        <h4 class="card-title mb-4">新建/编辑地址</h4>
                        <div class="form-row">
                            <div class="form-group ">
                                <label>收件人姓名</label>
                                <input type="text" class="form-control" value="" id="linkManName">
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group ">
                                <label>电话号码</label>
                                <input type="number"  class="form-control" value="" id="linkManMobile">
                            </div>
                        </div>

                        <div class="form-row">
                            <div class="form-group ">
                                <label>地址</label>
                                <input type="text" class="form-control" value="" id="addr">
                            </div>
                        </div>

                        <div class="form-row">
                            <div class="form-group ">
                                <label>是否默认</label>
                                <select class="form-control" id="isDef"><option value="1">是</option><option  value="0">否</option></select>
                            </div>
                        </div>

                        <button class="btn btn-primary" id="btn-add">保存</button>

                    </div>
                </article>
            </main>
        </div>
    </div>
</section>


<!-- script -->
<@netCommon.commonScript />


<script>
    var addressID;

    function getAddressDetail(){
        $.ajax({
            type: "GET",
            url: baseUrl+"api/user-center//app/cust/addr/get?id="+addressID,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            headers: {
                'lbv': '1.0',
                'tenant' : 'PTC',
                'token':  getToken(),
            },
            success: function (res) {
                debugger;
                if(res && res.code && res.code=='0'){
                    $('#linkManName').val(res.data.linkManName)
                    $('#linkManMobile').val(res.data.linkManMobile)
                    $('#addr').val(res.data.addr)
                }
                else{
                        alert(res.msg);
                        if(res.code=='-3'){
                            relogin(window.location.href);
                        }
                        
                    }
            },
            error: function (res) {

            }
        });
    }
    $(function () {
        addressID =  getQueryVariable('id')
        if(addressID!==false){
            getAddressDetail();
        }
        $("#btn-add").click(function () {

            if($('#linkManName').val() && $('#linkManName').val()==''){
                alert('请设置收件人姓名')
                return;
            }
            if($('#linkManMobile').val() && $('#linkManMobile').val()==''){
                alert('请设置收件人手机')
                return;
            }
            if($('#addr').val() && $('#addr').val()==''){
                alert('请设置收件人地址')
                return;
            }
            if(addressID!==false){
                update();
            }else {
                save();
            }

        })
    })
        //过滤非法字符
        		function stripscript(s){
        			var pattern = new RegExp("[`~!#$^&*()=|{}':;',\\[\\]<>/?~！#￥……&*（）——|{}【】‘；：”“'。，、？]")
        			var rs = "";
        			for (var i = 0; i < s.length; i++) {
        				rs = rs+s.substr(i, 1).replace(pattern, '');
        			}
        		return rs;
        		}

    function update() {
        var param = {
            linkManName : stripscript($('#linkManName').val()) ,
            linkManMobile :stripscript($('#linkManMobile').val()) ,
            addr :  stripscript($('#addr').val()) ,
            isDef: $('#isDef').val(),
            id:addressID
        }

        $.ajax({
            type: "POST",
            url: baseUrl+"api/user-center/app/cust/addr/update",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            headers: {
                'lbv': '1.0',
                'tenant' : 'PTC',
                'token':  getToken(),
            },
            data: JSON.stringify(param),
            success: function (res) {
                debugger;
                if(res && res.code && res.code=='0'){
                    alert('修改成功')
                    //window.history.back(-1);
                    window.history.back(-1);
                }
                else{
                        alert(res.msg);
                        if(res.code=='-3'){
                            relogin(window.location.href);
                        }
                        
                    }
            },
            error: function (res) {

            }
        });
    }

    function save() {
        var param = {
            linkManName : stripscript($('#linkManName').val()) ,
            linkManMobile :stripscript($('#linkManMobile').val()) ,
            addr :  stripscript($('#addr').val()) ,
            isDef: $('#isDef').val()
        }

        $.ajax({
            type: "POST",
            url: baseUrl+"api/user-center/app/cust/addr/save",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            headers: {
                'lbv': '1.0',
                'tenant' : 'PTC',
                'token':  getToken(),
            },
            data: JSON.stringify(param),
            success: function (res) {
                debugger;
                if(res && res.code && res.code=='0'){
                    alert('保存成功')
                    //window.history.back(-1);
                    window.history.back(-1);
                }
                else{
                        alert(res.msg);
                        if(res.code=='-3'){
                            relogin(window.location.href);
                        }
                        
                    }
            },
            error: function (res) {

            }
        });
    }



</script>



</body>
</html>
